﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Molen_Ameland
{
    class Data
    {
        string logfile;
        public Data(string LogFile)
        {
            logfile = LogFile;
        }
        //This struct contains all the data available in the log file
        public struct Statistics
        {
            public DateTime currentTime;
            public double voltage;
            public double power;
            public double energy;
            public double temperature;
            public double windspeed1;
            public double windspeed2;
            public double frequency;

            public Statistics(DateTime currentTime, double voltage, double power, double energy, double temperature, double windspeed1, double windspeed2, double frequency)
            {
                this.currentTime = currentTime;
                this.voltage = voltage;
                this.power = power;
                this.energy = energy;
                this.temperature = temperature;
                this.windspeed1 = windspeed1;
                this.windspeed2 = windspeed2;
                this.frequency = frequency;
            }
        }
        /// <summary>
        /// this method reads the first line of the .txt file and deletes it afterwords
        /// </summary>
        /// <param name="count">//temporary parameter to emulate data progression</param>
        /// <returns>this method returns a List with all the data from the .txt file in strings</returns>
        public List<string> readTextfile(int count)
        {
            Form1 p = new Form1();
            p.saveLogFile();
            string textFileLine = File.ReadLines(logfile).Skip(1).Take(1).First();
            int position = 0;
            List<string> devidedData = new List<string>();
            for (int i = 0; i < textFileLine.Length; i++)
            {
                if (textFileLine[i] == ';')
                {
                    devidedData.Add(textFileLine.Substring(position, i-position));
                    position = i + 1;
                }
            }
            //System.Threading.Thread.Sleep(100);
            deleteLog();
            return devidedData;

        }
        /// <summary>
        /// this method converts all the string data to double
        /// </summary>
        /// <param name="count">//temporary parameter to emulate data progression</param>
        /// <returns>this method returns a struct with all the data</returns>
        public Statistics getData (int count)
        {
            List<string> dataList = readTextfile(count);
            List<double> convertedData = new List<double>();
            for (int i = 1; i < 8; i++)
            {
                convertedData.Add(Double.Parse(dataList.ElementAt(i))); //p
            }
            Statistics p = new Statistics(Convert.ToDateTime(dataList.ElementAt(0)), convertedData.ElementAt(0), convertedData.ElementAt(1), convertedData.ElementAt(2), convertedData.ElementAt(3), convertedData.ElementAt(4), convertedData.ElementAt(5), convertedData.ElementAt(6));
            return p;
        }
        /// <summary>
        /// Method that returns the working directory of the multitool process
        /// </summary>
        /// <returns>this method returns the working directory</returns>
        public static string getPath()
        {
            string exepath = ""; //the pathname + the executable file
            string workdir = ""; //multitool pathname only (trimmed)
            foreach (Process PPath in Process.GetProcesses())
            {
                if (PPath.ProcessName.ToString() == "MultiTool")
                {
                    exepath = PPath.MainModule.FileName;
                }
            }
            workdir = exepath.TrimEnd('e', 'x', '.', 'l', 'o', 'T', 'i', 't', 'l', 'u', 'M');
            Properties.Settings.Default.exepath = exepath; //save the executable path in the settings file
            Properties.Settings.Default.Save(); // saves the settings
            return workdir;
        }
        /// <summary>
        /// This method deletes the log.txt file
        /// </summary>
        void deleteLog()
        {
            if (File.Exists(Data.getPath() + "log.txt"))
            {
                File.Delete(Data.getPath() + "log.txt");
            }
        }
    }
}
